import { DeviceService } from './../../../../@core/services/device.ts/device.service';
import { Component,  } from '@angular/core';
import { Router } from '@angular/router';
import { CascaderItem, DValidateRules, FormLayout, } from 'ng-devui';
import { EncodeRulesService } from 'src/app/@core/services/encoderRule/encode-rules.service';
@Component({
  selector: 'app-add-device',
  templateUrl: './add-device.component.html',
  styleUrls: ['./add-device.component.scss']
})
export class AddDeviceComponent {
  msg:any;
  layoutDirection: FormLayout = FormLayout.Horizontal;
  options: any=[];
  status:any=['报废','在用'];
  chosenItem = '在用';
  deviceLevel:any=['一般','重要','关键'];
  option: Array<number> = [];//设备分类
  purchaseDate: Date
  productionDate: Date
  assemblyTime: Date
  device = {
    equipmentName: "",
    equipmentNum: "",
    equipmentCategoryId: null,
    model: "",
    site: "",
    purchaseDate: "",
    productionDate: "",
    assetsCode: "",
    assemblyTime: "",
    isEnabled: true,
    level: "",
    isScheduling: true,
    workshop: "",
    maintenanceTypeId: 0,
    note: "",
    priority: null
  }
  msgs: Array<Object> = [];
  message: Array<Object> = [];
  DeviceRules:{[key:string]:DValidateRules} = {
    partNum: {
      messageShowType:'text',
      validators: [
        { required: true ,message: '*物料编码不能为空' },
        { minlength: 2, message: '*物料编码长度至少为2' },
        { maxlength: 40, message: '*物料编码长度至多为40' },
        { pattern: /^[^\s]*$/, message: '*物料编码不能包含空格' }
      ],
    },
    partName: {
      messageShowType:'text',
      validators: [
        { required: true ,message: '*物料名称不能为空' },
        { maxlength: 40, message: '*物料名称长度至多为40' },
        { pattern: /^[^\s]*$/, message: '*物料名称不能包含空格' }
      ],
    },
    englishName: {
      messageShowType:'text',
      validators: [
        { maxlength: 40, message: '*英文名称长度至多为40' },
        { pattern: /^[^\s]*$/, message: '*英文名称不能包含空格' }
      ],
    },
    partType: {
      messageShowType:'text',
      validators: [
      ],
    },
    mnemonicCode: {
      messageShowType:'text',
      validators: [
        { maxlength: 40, message: '*助记码长度至多为40' },
        { pattern: /^[^\s]*$/, message: '*助记码不能包含空格' }
      ],
    },
    partClassid: {
      messageShowType:'text',
      validators: [
        { required: true ,message: '*物料类别不能为空' },
      ],
    },
    wareName: {
      messageShowType:'text',
      validators: [
        { required: true ,message: '*仓库不能为空' },
      ],
    },
    positionName: {
      messageShowType:'text',
      validators: [
        { required: true ,message: '*库位不能为空' },
      ],
    },
    description: {
      messageShowType:'text',
      validators: [
        { maxlength: 200, message: '*物料描述长度至多为200' },
        { pattern: /^[^\s]*$/, message: '*物料描述不能包含空格' }
      ],
    },
    costPrice: {
      messageShowType:'text',
      validators: [
        { pattern: /^[1-9][0-9]*$/, message: '*成本价只能为数字' }
      ],
    },
    salePrice: {
      messageShowType:'text',
      validators: [
        { pattern: /^[1-9][0-9]*$/, message: '*销售价只能为数字' }
      ],
    },
    partProperty: {
      messageShowType:'text',
      validators: [
        { required: true ,message: '*物料属性不能为空' },
      ],
    },
    partUOM: {
      messageShowType:'text',
      validators: [
        { required: true ,message: '*物料单位不能为空' },
      ],
    },
    minimumStock: {
      messageShowType:'text',
      validators: [
        { required: true ,message: '*最小库存不能为空' },
        { pattern: /^[1-9][0-9]*$/, message: '*最小库存只能为数字' }
      ],
    },
    maximumStock: {
      messageShowType:'text',
      validators: [
        { required: true ,message: '*最大库存不能为空' },
        { pattern: /^[1-9][0-9]*$/, message: '*最大库存只能为数字' }
      ],
    },
    safetyInventory: {
      messageShowType:'text',
      validators: [
        { required: true ,message: '*安全库存不能为空' },
        { pattern: /^[1-9][0-9]*$/, message: '*安全库存只能为数字' }
      ],
    },
  }
  file: any;
  constructor(private EncodeRulesService:EncodeRulesService,private Router:Router,private DeviceService:DeviceService){
  }
  ngOnInit(): void {
   this.EncodeRulesService.GenerateWarehouseCodeRule('设备编码','设备编码').subscribe((res: any) => {
     this.device.equipmentNum = res;
   });
   this.DeviceService.GetPagedEquipmentCategoriesWithChildren(1, 999).subscribe((res: any) => {
     this.options = this.convertToCascaderItem(res.items);
   });
   this.device.isEnabled=this.statusToBoolean(this.chosenItem);
  }
  convertToCascaderItem(data: any[]): CascaderItem[] {
    return data.map(item => {
      let cascaderItem: CascaderItem = {
        label: item.equipmentCategoryName,
        value: item.equipmentCategoryId,
      };
      if (item.subEquipmentCategories) {
        cascaderItem.children = this.convertToCascaderItem(item.subEquipmentCategories);
      }
      return cascaderItem;
    });
  }//转换数据格式为华为库练级菜单的数据格式
  CascaderonChanges($event:any){
    this.device.equipmentCategoryId = $event[$event.length-1]
  }
  submit(){
    this.DeviceService.CreateEquioment(this.device).subscribe((res: any) => {
      this.showToast('success','提示','添加成功');
      setTimeout(() => {
        this.Router.navigate(['/pages/device/device'])
      }, 1500);
    },(error) => {
      if(error.status==404){
        this.showToast('error', '失败', '服务器连接失败');
      }else{
        this.showToast('error', '失败', error.error);
      }
    }
    )
  }
  cancle(){
    this.Router.navigate(['/pages/device/device'])
  }
  showToast(type: any,summary:any,content:any) {
    switch (type) {
    default:
      this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  purchaseDateChange(){
   this.device.purchaseDate = this.purchaseDate.toISOString();
  }
  productionDateChange(){
   this.device.productionDate = this.productionDate.toISOString();
  }
  assemblyTimeChange(){
   this.device.assemblyTime = this.assemblyTime.toISOString();
  }
  statusChange(){
    this.device.isEnabled=this.statusToBoolean(this.chosenItem);
  }
  statusToBoolean(status: string): boolean {
    if (status === '在用') {
      return true;
    } else if (status === '报废') {
      return false;
    } else {
      throw new Error('Invalid status: ' + status);
    }
  }
}
